<template>
	<view class="my-settle-container">
		<label class="radio" @click="changeCheck">
			<radio color="#C00000" :checked="isFullChecked" /><text>全选</text>
		</label>
		<view class="amount-box">
			合计：<text class="amount">¥{{checkedGoodsAmount}}</text>
		</view>
		<view class="btn-settle" @click="settlement">
			结算({{checkCount}})
		</view>
	</view>
</template>

<script>
	import {mapGetters,mapMutations,mapState} from 'vuex'
	export default {
		name:"my-settle",
		data() {
			return {
				second:3,
				timer:null
			};
		},
		computed:{
			...mapGetters('m_cart',['checkCount','total','checkedGoodsAmount']),
			...mapGetters('m_user',['addstr']),
			...mapState('m_user',['token']),
			...mapState('m_cart',['cart']),
			isFullChecked(){
				return this.total == this.checkCount
			}
		},
		methods:{
			...mapMutations('m_cart',['updataGoodsAllState']),
			changeCheck(){
				this.updataGoodsAllState(!this.isFullChecked)
			},
			settlement(){
				if(!this.checkCount) return uni.$showMsg('请选择要结算的商品！')
				if(!this.addstr) return uni.$showMsg('请选择收获地址！')
				// if(!this.token) return uni.$showMsg('请先登录！')
				if(!this.token) return this.delayNaviate()
				
				this.payOrder()
			},
			async payOrder(){
				const orderInfo = {
					order_price : 0.01,
					consignee_addr: this.addstr,
					goods:this.cart.filter(x => x.goods_state).map(x => ({
						goods_id:x.goods_id,
						goods_number:x.goods_count,
						goods_price:x.goods_price,
						// Authorization:'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo'
					}))
				}
				const {data:res} = await uni.$http.post('/api/public/v1/my/orders/create',orderInfo)
				if(res.meta.status !== 200) return uni.$showMsg('创建订单失败！')
				const orderNumber = res.message.order_number
				const {data:res2} = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder',{'order_number':orderNumber})
				if(res2.meta.status !== 200) return uni.$showMsg('创建订单失败！')
				const payInfo = res2.message.pay
				
				
				// 发起微信支付
				
				const [err,succ] = await uni.requestPayment(payInfo)
				if(err) return uni.$showMsg('订单支付失败！')
				
				const {data:res3} = await uni.$http.post('/api/public/v1/my/orders/chkOrder',{'order_number':orderNumber})
				if(res3.meta.status !== 200) return uni.$showMsg('订单支付失败！')
				
				uni.showModal({
					title:'完成支付！',
					icon:'success'
				})
			},
			showTip(n){
				uni.showToast({
					icon:'none',
					title:'请登录后结算'+n+'秒后自动跳转到登录页',
					mask:true,
					duration:1500
				})
			},
			delayNaviate(){
				this.second = 3
				this.showTip(this.second)
				this.timer = setInterval(()=>{
					this.second--
					
					if(this.second <= 0){
						clearInterval(this.timer)
						uni.switchTab({
							url:'/pages/my/my'
						})
						return
					}
					
					this.showTip(this.second)
					
					
				},1000)
			}
		}
		
	}
</script>

<style lang="scss">
	.my-settle-container{
		position: fixed;
		bottom: 0;
		left: 0;
		height: 50px;
		width: 100%;
		background-color: white;
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 14px;
		padding-left: 5px;
		.radio{
			display: flex;
			align-items: center;
		}
		.amount-box{
			.amount{
				color: #C00000;
				font-weight: bold;
			}
		}
		.btn-settle{
			background-color: #C00000;
			height: 50px;
			color: white;
			line-height: 50px;
			padding: 0 10px;
			min-width: 100px;
			text-align: center;
		}
	}
</style>